{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2bd04b40",
   "metadata": {},
   "source": [
    "1.基于data_class_raw.csv数据，根据高斯分布概率密度函数，寻找异常点并剔除  \n",
    "2.基于data_class_processed.csv数据，进行PCA处理，确定重要数据维度及成分  \n",
    "3.完成数据分离  \n",
    "4.建立KNN模型完成分类，n_neighbors=10，计算分类准确率，可视化分类边界\n",
    "5.计算测试数据集对应的混淆矩阵  \n",
    "6.更改n_neighbors并查看效果  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ef081761",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.77</td>\n",
       "      <td>3.97</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.71</td>\n",
       "      <td>2.81</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.18</td>\n",
       "      <td>1.31</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.80</td>\n",
       "      <td>0.69</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.21</td>\n",
       "      <td>1.14</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     x1    x2  y\n",
       "0  0.77  3.97  0\n",
       "1  1.71  2.81  0\n",
       "2  2.18  1.31  0\n",
       "3  3.80  0.69  0\n",
       "4  5.21  1.14  0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = pd.read_csv('data_class_raw.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "601b1e8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = data.drop(['y'],axis = 1)\n",
    "y = data.loc[:,'y']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1cbff62f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAATIAAAEvCAYAAADGjk2AAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAVNElEQVR4nO3db2xcV5nH8d+ziVEmaeqpmryJHepUAjerOFEqV7RN1a4akKWFtCFaVSC1ZFuJ8GehBSGjZF90S3mRoEQUIgEiKooKG4qi1LKUZkVYNZUgq6rCqSunbdZvoKSeFBGiOJB2vLjh2RfjSWN3bM947p85934/EnK5TMbPDdPfnHPuc+8xdxcAhOwf0i4AAJpFkAEIHkEGIHgEGYDgEWQAgkeQAQje4jjedMWKFd7V1RXHWwPIsVOnTv3Z3VfOPB5LkHV1dWloaCiOtwaQY2b2h1rHmVoCCB5BBiB4BBmA4MWyRgYgWpOTkxobG9PExETapSRiyZIl6uzsVFtbW12vJ8iAAIyNjWn58uXq6uqSmaVdTqzcXRcuXNDY2JjWrFlT159hagkEYGJ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pnZg+lWlSyCbAYza1MlxA65+0Da9cRgk6T7zOxNSb+QdK+Z/We6JcVmTNKYu1dH1UdUCbas+bik37v7eXeflDQg6c6Ua0oUQXYNMzNV1lPOuPt3064nDu6+y9073b1LlQXhE+6eyW9vd/+jpLfMrLqZ52ZJb6RYUlzOSrrdzJZOfYY3K4MXNebCdnDTbZL0kKTTZvbq1LF/d/f/SrEmNOerkg6Z2Yck/U7SwynXEzl3f9nMjkh6RZUr78PKWYc/nf0AgsfUEkDwCDIAwSPIAASPIAMQPIIMQPAIMgDBI8gABI8gAxC8/wer+XI16BJknwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "fig1 = plt.figure(figsize=(5,5))\n",
    "bad = plt.scatter(x.loc[:,'x1'][y==0],x.loc[:,'x2'][y==0])\n",
    "good = plt.scatter(x.loc[:,'x1'][y==1],x.loc[:,'x2'][y==1])\n",
    "plt.legend((good,bad),('good','bad'))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "524d8081",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 -1]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.covariance import EllipticEnvelope\n",
    "ad_model = EllipticEnvelope(contamination=0.02)\n",
    "ad_model.fit(x[y==0])\n",
    "y_predict_bad = ad_model.predict(x[y==0])\n",
    "print(y_predict_bad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dbe63739",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAATIAAAEvCAYAAADGjk2AAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RV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e4u7t6q4IbzP3TP56e3uf5X0tpmVDvNcJunNFEuKy1uSvmhm0y7+Di9TBi9qjIXj4IZbLOluSYfN7PWLY//h7v+dYk2ozX2StpnZFEl/lnRPyvVEzt1fMbMdkl5T8cp7t3LW4U9nP4DgsbQEEDyCDEDwCDIAwSPIAASPIAMQPIIMQPAIMgDBI8gABO//ASmYCv/OalrGAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig2 = plt.figure(figsize=(5,5))\n",
    "bad = plt.scatter(x.loc[:,'x1'][y==0],x.loc[:,'x2'][y==0])\n",
    "good = plt.scatter(x.loc[:,'x1'][y==1],x.loc[:,'x2'][y==1])\n",
    "plt.scatter(x.loc[:,'x1'][y==0][y_predict_bad==-1],x.loc[:,'x2'][y==0][y_predict_bad==-1],marker = 'x',s = 150)\n",
    "plt.legend((good,bad),('good','bad'))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7c3994cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('data_class_processed.csv')\n",
    "data.head()\n",
    "\n",
    "x = data.drop(['y'],axis = 1)\n",
    "y = data.loc[:,'y']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "41ad4763",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.5369408 0.4630592]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler # 标准化处理\n",
    "from sklearn.decomposition import PCA # 降维处理\n",
    "x_norm = StandardScaler().fit_transform(x)\n",
    "pca = PCA(n_components = 2) #n_components指数据降为几维\n",
    "x_reduced = pca.fit_transform(x_norm)\n",
    "var_ratio = pca.explained_variance_ratio_\n",
    "print(var_ratio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cb364706",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9snjFwNfX/n85nXeDJxlrufNej9u1Cu8IbfDTbsL+NhmDVNVnwO+dZUlq92ad63PY8OzVNVDVfXtyebnWf4zlZtmwGuzmrm/Nits9jnzjar64uTxfwJf5fl3L83lvBkyy7zPm/W6UYM3+Fa3JD8KHAI+PrW7gM8meXhyd8hmW23erb5l710sX0E8Z96vy3NeneRLSR5Icutk35a9NvM+Z5LsBX4R+MKKQ3M/b64yy7Tr5bx5nkF/LOUFaNCtbhNvBv6hqqZ/Z39tVV1K8jPAXyf52uR3/82y2rzX8jxGleQNLJ+4r5vaPe/XBeCLwMur6pkkdwKfYvlv5dmy14Y5njNJfozlsL63qp5eeXjGL9m082aNWZ5bc72cNzPdqFd4g251mzjCircmVXVp8s8ngU+y/BZhM60277U8j9EkuQ34CHC4qp56bv8WvC5U1dNV9czk8Rng5iQ72KLXZmIu50ySm1kOzJ9X1SdmLJnbeTNgluvqvFnVVn8TcTM+WL5yvQjs4wfftL11xrqfYPl7Ni+a2vci4MVTjx8CDo0w015W/8b8m/jhbz7/07U8j5FnuYXlO2Zes2L/prwuA+Z5KT/486IHgX+bvE5zf23mec5MnuNHgQ9eZc1czpuBs8z9vFnPxw35lraG3Q4H8Fbgs1X1X1O//GeBTyaB5RPnL6rqrzYyT5KPsfzTxh1JloA/Bm6emmXmrXmrPY9NnuVe4CXAhyevwZVaviF89Ndl4DxvA96T5ArwXeBILf/XsxWvDczpnAFeC7wD+HKSRyb7/ojlsMz7vBkyy1zPm/XyTgtJbdyo38OTpOcxeJLaMHiS2jB4ktoweJLaMHiS2jB4ktoweJLa+D/vgxO1l39J/wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig4 = plt.figure(figsize=(5,5))\n",
    "plt.bar([1,2],var_ratio)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "37763ca3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(21, 2) (14, 2) (35, 2)\n"
     ]
    }
   ],
   "source": [
    "# 数据分离\n",
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=4,test_size=0.4)\n",
    "print(x_train.shape,x_test.shape,x.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "caed4754",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9047619047619048 0.6428571428571429\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn_10  = KNeighborsClassifier(n_neighbors = 10)\n",
    "knn_10.fit(x_train,y_train)\n",
    "y_train_predict = knn_10.predict(x_train)\n",
    "y_test_predict = knn_10.predict(x_test)\n",
    "# 计算准确率\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy_train = accuracy_score(y_train,y_train_predict)\n",
    "accuracy_test = accuracy_score(y_test,y_test_predict)\n",
    "print(accuracy_train,accuracy_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5167904",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可视化分类边界\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
